package com.fingard.dsp.bank.reqrespobj.authcancelforrec;

import com.fingard.dsp.bank.reqrespobj.ReqRespHeader;
import com.fingard.dsp.bank.reqrespobj.RequestBase;
import com.fingard.dsp.bank.reqrespobj.authforrec.detail.ReqAuthForRecDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;

public class ReqAuthCancelForRec extends RequestBase {
    public String totalNum = "";
    public String reqSeqID = "";
    public String payType = "";
    public String ownActName = "";
    public String ownAct = "";
    public String reqReserved1 = "";
    public String reqReserved2 = "";

    private XmlTextReader xmlReader;
    private String detailString;

    public int detailCount = -1;

    /**
     * 协议编号
     */
    public String authAgNum;
    /**
     * 缴费种类
     */
    private String authAgName;

    /**
     * 获取缴费种类
     */
    public String getAuthAgName() {
        if (StringHelper.hasAnyChar(payType)) {
            return payType;
        } else if (StringHelper.hasAnyChar(authAgName)) {
            return authAgName;
        }
        return "";
    }


    public ReqAuthCancelForRec(XmlTextReader p_xmlReader, ReqRespHeader p_header) throws Exception {
        reqHeader = p_header;
        xmlReader = p_xmlReader;
        totalNum = xmlReader.getSimpleText("TotalNum");
        reqSeqID = xmlReader.getSimpleText("ReqSeqID");
        payType = xmlReader.getSimpleText("PayType");
        authAgNum = xmlReader.getSimpleTextNotNull("AuthAgNum");
        authAgName = xmlReader.getSimpleTextNotNull("AuthAgName");
        ownActName = xmlReader.getSimpleText("AcctName");
        ownAct = xmlReader.getSimpleText("AcctNo");
        reqReserved1 = xmlReader.getSimpleText("ReqReserved1");
        reqReserved2 = xmlReader.getSimpleText("ReqReserved2");
        detailString = xmlReader.getSimpleText("DetailRecord");
        beginIndex = 0;
        endIndex = 0;

        /** ATS报文明细 DetailRecord中每笔明细末尾加\n分割,最后一笔不加\n*/
        if (StringHelper.hasAnyChar(detailString)) {
            if (detailString.contains("\n")) {
                detailCount = detailString.split("\n").length;
            } else {
                detailCount = 1;
            }
        } else {
            detailCount = Integer.valueOf(totalNum);
        }

    }

    public ReqAuthForRecDetail readedDetail;
    private int beginIndex;
    private int endIndex;

    public boolean readRecord() throws Exception {
        readedDetail = null;
        if (!detailString.contains("||")) {
            if (xmlReader.read(PATH_Detail)) {
                readedDetail = new ReqAuthForRecDetail(xmlReader.readedXElement);
                return true;
            } else {
                return false;
            }
        } else {
            if (endIndex < 0) {
                return false;
            }
            int lenStr = detailString.length();
            while (endIndex < lenStr) {
                if (detailString.charAt(endIndex) == '\n' || endIndex == lenStr - 1) {
                    String subStr = detailString.substring(beginIndex, endIndex + 1);
                    readedDetail = new ReqAuthForRecDetail(subStr);
                    beginIndex = endIndex + 1;
                    endIndex = beginIndex;
                    return true;
                }
                if (endIndex < lenStr - 1) {
                    endIndex++;
                }
            }
            beginIndex = -1;
            endIndex = -1;
            return false;
        }
    }
}
